package customer.design.dispatcher.dispatchermode.controller;

import customer.design.dispatcher.dispatchermode.bean.BaseResponse;
import customer.design.dispatcher.dispatchermode.bean.CaseRequest;
import customer.design.dispatcher.dispatchermode.common.CaseEnum;
import customer.design.dispatcher.dispatchermode.common.StatusCode;
import customer.design.dispatcher.dispatchermode.service.CaseInterface;
import customer.design.dispatcher.dispatchermode.util.InitCaseBeanMapComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author ldanniel
 */
@RestController
@RequestMapping("/switch")
public class BaseController {
    @Autowired
    private InitCaseBeanMapComponent mapComponent;

    @GetMapping("/caseInfo")
    public BaseResponse caseInfo(@RequestParam String type, @RequestParam String name){
        BaseResponse response=new BaseResponse(StatusCode.Success);
        try {
            CaseRequest request=new CaseRequest(type,name);

            CaseEnum caseEnum= CaseEnum.valueOf(request.getType());
            CaseInterface caseInterface= mapComponent.getProcessMap().get(caseEnum);
            response.setData(caseInterface.execute(request));
        }catch (Exception e){
            response=new BaseResponse(StatusCode.Failure.getCode(),e.getMessage());
        }
        return response;
    }
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String hello(){
        return "hello";
    }
}
